package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.bean.WorkTimeBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.EmployeeDao;
import com.sp.scmdb.dao.WorkTimeDao;
import com.sp.scmdb.entity.WorkTime;
import com.sp.scmdb.service.LogService;
import com.sp.scmdb.service.WorkTimeService;

@Service("workTimeService")
public class WorkTimeServiceImpl implements WorkTimeService {

	private WorkTimeDao workTimeDao;
	private EmployeeDao empDao;
	private LogService logs;

	public void setLogs(LogService logs) {
		this.logs = logs;
	}
	
	public void setEmpDao(EmployeeDao empDao) {
		this.empDao = empDao;
	}

	public void setWorkTimeDao(WorkTimeDao workTimeDao) {
		this.workTimeDao = workTimeDao;
	}

	@Transactional(readOnly = true)
	public List<WorkTimeBean> queryAll() {
		List<WorkTime> list = workTimeDao.queryAll();
		List<WorkTimeBean> listBean = new ArrayList<WorkTimeBean>();
		WorkTimeBean bean = null;
		for (int i = 0; i < list.size(); i++) {
			bean = new WorkTimeBean();
			bean.setId(list.get(i).getId().toString());
			bean.setName(list.get(i).getName());
			bean.setType(list.get(i).getType());
			if ("0".equals(list.get(i).getType())) {
				bean.setType("最小工时");
			} else {
				bean.setType("最大工时");
			}
			bean.setHourLen(list.get(i).getHourLen());
			
			listBean.add(bean);
		}
				
		return listBean;
	}

	@Transactional
	public String newWT(WorkTimeBean bean, UserBean user) {
		String result="3";
		StringBuffer Hql = new StringBuffer("from WorkTime where 1=1 ");
		if (StringUtils.isNotEmpty(bean.getName())) {
			Hql.append("and name='").append(bean.getName()).append("'");
			List<WorkTime> list = workTimeDao.queryHql(Hql.toString());
			if (CollectionUtils.isEmpty(list)) {
				WorkTime workTime = new WorkTime();
				workTime.setName(bean.getName());
				workTime.setType(bean.getType());
				workTime.setHourLen(bean.getHourLen());
				workTimeDao.save(workTime);
				logs.addLog(user, "添加 工时：\""+bean.getName()+"\"成功！");	
				
				result = CommonParamter.CHECK_SUCCESS;
			}
			else{
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	@Transactional
	public String editWT(WorkTimeBean bean, UserBean user) {
		String result = "2";
		WorkTime workTime = workTimeDao.getById(Integer.parseInt(bean.getId()));
		if (null != workTime) {
			// 检查角色名称是否重复
			if (StringUtils.isNotEmpty(bean.getName())) {
				StringBuffer Hql = new StringBuffer();
				Hql.append("from WorkTime where id != " + bean.getId()
						+ " and name = '" + bean.getName() + "'");
				if (CollectionUtils.isEmpty(workTimeDao.queryHql(Hql.toString()))) {					
					// 如果名称没有重复的
					workTime.setName(bean.getName());
					workTime.setType(bean.getType());
					workTime.setHourLen(bean.getHourLen());
					
					// 保存该角色
					workTimeDao.update(workTime);
					logs.addLog(user, "修改 工时：\""+bean.getName()+"\"成功！");	
					result = CommonParamter.CHECK_SUCCESS;
				} else {
					result = CommonParamter.CHECK_REPEAT;
				}
			}
		}
		return result;
	}

	@Transactional
	public String delWT(String[] ids, UserBean user){
		String result="1";
		WorkTime workTime = null;
		for (int i = 0; i < ids.length; i++) {
			workTime = workTimeDao.getById(Integer.parseInt(ids[i]));
			if (workTime != null) {
				// 判断如果该工时分配给了人员，则不能删除
				if (CollectionUtils.isNotEmpty(empDao.queryHql("from Employee where empWorkTime="+workTime.getId()+" or empMaxWorkTime="+workTime.getId()+""))) {
					return "10";
				}
				logs.addLog(user, "删除 工时：\""+workTime.getName()+"\"成功！");	
				workTimeDao.delete(workTime);
				result = CommonParamter.CHECK_SUCCESS;
			}
		}
		return result;
	}

	@Transactional(readOnly = true)
	public WorkTime findID(String depId) {
		
		return workTimeDao.getById(Integer.parseInt(depId));
	}
}
